/*
 * Copyright (C) 2012-2013  Romain DUBOIS
 * 
 * This file is part of NeMoS - Network and Modular Software
 * 
 * NeMoS is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * NeMoS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with NeMoS.  If not, see <http://www.gnu.org/licenses/>.
 */
package nemos.remote.rsa.message.impl;

import nemos.remote.rsa.message.transport.ITransport;

import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceFactory;
import org.osgi.framework.ServiceRegistration;
import org.osgi.service.remoteserviceadmin.RemoteServiceAdmin;

/**
 * TransportRsaFactory : The transport RSA factory service.
 * 
 * @author Romain DUBOIS
 */
public class TransportRsaFactory implements ServiceFactory<RemoteServiceAdmin> {

    /** The transport to use */
    private final ITransport mTransport;
    /** The proxy factory */
    private final GenericRemoteServiceFactory mProxyFactory;
    /** The factory registration */
    private ServiceRegistration<?> mRegistration;

    /**
     * Constructor
     * 
     * @param pTransport
     *            The transport to use
     */
    public TransportRsaFactory(final ITransport pTransport) {
        super();

        mTransport = pTransport;
        mProxyFactory = new GenericRemoteServiceFactory(mTransport);
    }

    /**
     * Start the factory.
     * 
     * @param pContext
     *            The bundle context
     */
    public synchronized void start(final BundleContext pContext) {
        mRegistration = pContext.registerService(RemoteServiceAdmin.class.getName(), this, null);
    }

    /**
     * {@inheritDoc}
     * 
     * @see org.osgi.framework.ServiceFactory#getService(org.osgi.framework.Bundle,
     *      org.osgi.framework.ServiceRegistration)
     */
    @Override
    public RemoteServiceAdmin getService(final Bundle pBundle,
            final ServiceRegistration<RemoteServiceAdmin> pRegistration) {
        final TransportRsa lRsa = new TransportRsa(mTransport.getId(), mProxyFactory);
        lRsa.setBundle(pBundle);
        lRsa.start(pRegistration.getReference());
        return lRsa;
    }

    /**
     * {@inheritDoc}
     * 
     * @see org.osgi.framework.ServiceFactory#ungetService(org.osgi.framework.Bundle,
     *      org.osgi.framework.ServiceRegistration, java.lang.Object)
     */
    @Override
    public void ungetService(
            @SuppressWarnings("unused") final Bundle pBundle,
            @SuppressWarnings("unused") final ServiceRegistration<RemoteServiceAdmin> pRegistration,
            final RemoteServiceAdmin pService) {
        ((TransportRsa) pService).stop();
    }

    /**
     * Stop the factory.
     */
    public synchronized void stop() {
        if (mRegistration != null) {
            mRegistration.unregister();
            mRegistration = null;
        }
    }
}
